﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Agresso.Interface.Layer.Domain
{
    public class BunkerInventory
    {
        public string profit_centre_fullname = "";
        public int voyage_no = 0;
        public string headfixture_name = "";
        public decimal price = 0;
        public string quality = "";
        public decimal quantity = 0;
        public string description = "";
        public int order_ = 0;
        public string type = "";
        public string last_updated_by = "";
        public string operated_by = "";
        public DateTime last_updated_date = new DateTime(1900, 1, 1);
        public string VoyageStatus = "";

        public int BunkerInventory_id;
        public string pickup_flag = "0";
        public DateTime pickup_date = new DateTime(1900, 1, 1);
        public DateTime insert_date = DateTime.Now;
        public string remark = "";
        public string vessel_code = "";

        public DateTime end_voyage_date = new DateTime(1900, 1, 1);


        public BunkerInventory() { }
        public BunkerInventory(SqlDataReader rd, bool i) 
        {
            profit_centre_fullname = rd["profit_centre_fullname"] is DBNull ? profit_centre_fullname : rd["profit_centre_fullname"].ToString();
            voyage_no = rd["voyage_no"] is DBNull ? voyage_no : Convert.ToInt32(rd["voyage_no"]);
            headfixture_name = rd["headfixture_name"] is DBNull ? headfixture_name : rd["headfixture_name"].ToString();
            price = rd["price"] is DBNull ? price : Convert.ToDecimal(rd["price"]);
            quality = rd["quality"] is DBNull ? quality : rd["quality"].ToString();
            quantity = rd["quantity"] is DBNull ? quantity : Convert.ToDecimal(rd["quantity"]);
            description = rd["description"] is DBNull ? description : rd["description"].ToString();
            order_ = rd["order_"] is DBNull ? order_ : Convert.ToInt32(rd["order_"]);
            type = rd["type"] is DBNull ? type : rd["type"].ToString();
            last_updated_by = rd["last_updated_by"] is DBNull ? last_updated_by : rd["last_updated_by"].ToString();
            operated_by = rd["operated_by"] is DBNull ? operated_by : rd["operated_by"].ToString();
            last_updated_date = rd["last_updated_date"] is DBNull ? last_updated_date : Convert.ToDateTime(rd["last_updated_date"]);
            VoyageStatus = rd["VoyageStatus"] is DBNull ? VoyageStatus : rd["VoyageStatus"].ToString();
            vessel_code = rd["vessel_code"] is DBNull ? "" : rd["vessel_code"].ToString();
            end_voyage_date = rd["end_voyage_date"] is DBNull ? end_voyage_date : Convert.ToDateTime(rd["end_voyage_date"]);
            if (i)
            {
                BunkerInventory_id = rd["BunkerInventory_id"] is DBNull ? BunkerInventory_id : Convert.ToInt32(rd["BunkerInventory_id"]);
                pickup_flag = rd["pickup_flag"] is DBNull ? pickup_flag : rd["pickup_flag"].ToString();
                pickup_date = rd["pickup_date"] is DBNull ? pickup_date : Convert.ToDateTime(rd["pickup_date"]);
                insert_date = rd["insert_date"] is DBNull ? insert_date : Convert.ToDateTime(rd["insert_date"]);
                remark = rd["remark"] is DBNull ? remark : rd["remark"].ToString();
            }
        }
        public BunkerInventory(SqlDataReader rd)
        {
            voyage_no = rd["voyage_no"] is DBNull ? voyage_no : Convert.ToInt32(rd["voyage_no"]);
            headfixture_name = rd["headfixture_name"] is DBNull ? headfixture_name : rd["headfixture_name"].ToString();
            profit_centre_fullname = rd["profit_centre_fullname"] is DBNull ? profit_centre_fullname : rd["profit_centre_fullname"].ToString();
            pickup_date = rd["pickup_date"] is DBNull ? pickup_date : Convert.ToDateTime(rd["pickup_date"]);
            remark = rd["remark"] is DBNull ? remark : rd["remark"].ToString();
            description = rd["description"] is DBNull ? description : rd["description"].ToString();
        }
        public static BunkerInventory CreateBunkerInventoryForComparation(SqlDataReader rd)
        {
            BunkerInventory b = new BunkerInventory();
            b.profit_centre_fullname = rd["profit_centre_fullname"] is DBNull ? "" : rd["profit_centre_fullname"].ToString();
            b.voyage_no = rd["voyage_no"] is DBNull ? 0 : Convert.ToInt32(rd["voyage_no"]);
            b.headfixture_name = rd["headfixture_name"] is DBNull ? "" : rd["headfixture_name"].ToString();
            b.quality = rd["quality"] is DBNull ? "" : rd["quality"].ToString();
            b.description = rd["description"] is DBNull ? "" : rd["description"].ToString();
            b.type = rd["type"] is DBNull ? "" : rd["type"].ToString();
            return b;
        }
        public string GetTransactionType()
        {
            if ((description.ToLower() == "bunkers on delivery to tc") || (description.ToLower() == "total consumption"))
                return "46";
            return "49";
        }
        public string GetVoucherType()
        {
            if ((description.ToLower() == "bunkers on delivery to tc") || (description.ToLower() == "total consumption"))
                return "IW";
            return "IR";
        }
        public decimal GetAmount()
        {
            if ((description.ToLower() == "bunkers on delivery to tc") || (description.ToLower() == "total consumption"))
                return 0;
            return quantity * price;
        }
        public string GetAccountCode(string article, string deliveryAcc, string redeliveryAcc)
        {
            switch (description.ToLower())
            {
                case "total consumption":
                    return article.Substring(0, 4);
                case "bunkers on delivery to tc":
                    return deliveryAcc;//"6231";
                case "bunkers on redelivery from tc":
                    return redeliveryAcc;//"3334";
            }
            return "";
        }
        public override bool Equals(object obj)
        {
            if(!(obj is BunkerInventory)) return false;
            BunkerInventory objb = (BunkerInventory)obj;
            bool a = profit_centre_fullname.Trim() == objb.profit_centre_fullname.Trim();
            bool b = voyage_no == objb.voyage_no;
            bool c = headfixture_name.Trim() == objb.headfixture_name.Trim();
            bool d = quality.Trim() == objb.quality.Trim();
            bool e = description.Trim() == objb.description.Trim();
            bool f = type.Trim() == objb.type.Trim();
            return a && b && c && d && e && f;
        }
    }
}
